﻿Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration
Imports System.Collections.Generic
Imports System.Xml.Serialization
Imports System.IO
Imports System.Diagnostics



Public Class CPUTestLogic

    Public Shared Function SaveCPUData(ByRef allCPUData As List(Of CPUMeasurementTest)) As String

        Dim aSerializer As New XmlSerializer(allCPUData.GetType())
        Dim anEncoding As New System.Text.ASCIIEncoding()
        Dim aStream As MemoryStream = New MemoryStream()

        aSerializer.Serialize(aStream, allCPUData)

        Dim allBytes() As Byte = aStream.GetBuffer()


        aStream.Read(allBytes, 0, CInt(aStream.Length))
        aStream.Close()
        aStream.Dispose()

        Return anEncoding.GetString(allBytes)


    End Function


    Public Shared Function ParseCPUData(ByVal CPULine As String) As List(Of CPUMeasurementTest)

        Dim newMeasuringList As New List(Of CPUMeasurementTest)

        Dim aSerializer As New XmlSerializer(newMeasuringList.GetType())

        Dim anEncoding As New System.Text.ASCIIEncoding()

        Dim CPUStream As MemoryStream = New MemoryStream(anEncoding.GetBytes(CPULine))

        Using CPUStream
            newMeasuringList = CType(aSerializer.Deserialize(CPUStream), List(Of CPUMeasurementTest))
        End Using

        Return newMeasuringList

    End Function


    Public Shared Function DetermineServersToTest(ByRef aFarm As SPFarm) As List(Of String)

        Dim aServiceDBCollection As New SPDatabaseServiceCollection(aFarm)
        Dim ServerList As New List(Of String)


        For Each aServiceDb As SPDatabaseService In aServiceDBCollection

            For Each aServiceDBInstance As SPDatabaseServiceInstance In aServiceDb.Instances

                '#If Not Debug Then
                ' If Not aServiceDBInstance.GetType() Is GetType(SPDatabaseServiceInstance) Then
                '#End If
                ServerList.Add(aServiceDBInstance.Server.Name)


                '#If Not Debug Then
                ' End If

                '#End If
            Next

        Next

        Return ServerList


    End Function

    Public Shared Function DetermineAverageCPUUse(ByRef AllCPUData As List(Of CPUMeasurementTest)) As Dictionary(Of String, Double)

        Dim runningServerAverages As New Dictionary(Of String, Double)
        Dim NumberOfHits As New Dictionary(Of String, Integer)

        For Each aMeasurement As CPUMeasurementTest In AllCPUData

            If NumberOfHits.ContainsKey(aMeasurement.ServerName) = False Then

                NumberOfHits.Add(aMeasurement.ServerName, 1)
                runningServerAverages.Add(aMeasurement.ServerName, aMeasurement.CPUMeasurement)

            Else

                NumberOfHits(aMeasurement.ServerName) += 1
                runningServerAverages(aMeasurement.ServerName) = (runningServerAverages(aMeasurement.ServerName) + aMeasurement.CPUMeasurement) / NumberOfHits(aMeasurement.ServerName)


            End If

        Next

        Return runningServerAverages


    End Function




End Class
